﻿using System;
using System.Linq;
using System.Windows.Forms;
using ConfigurationManagement.Administration.Data;
using ConfigurationManagement.Data;

namespace ConfigurationManagement.Administration.UI.Utilities
{
    public partial class CopySolutionForm : Form
    {
        private readonly ConfigurationManagementDataContext _db = new ConfigurationManagementDataContext();
        private Project _selectedProject;
        private Solution _selectedSolution;

        public CopySolutionForm()
        {
            InitializeComponent();
        }

        #region Event Handlers

        private void ProjectsComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ProjectsComboBox.SelectedIndex < 0) return;
            _selectedProject = (Project) ProjectsComboBox.SelectedItem;
            LoadSolutions();
        }

        private void SolutionsComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            _selectedSolution = (Solution) SolutionsComboBox.SelectedItem;
        }

        private void CopySolutionFormLoad(object sender, EventArgs e)
        {
            LoadProjects();
        }

        private void CopySolutionButtonClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(NewNameTextBox.Text))
            {
                MessageBox.Show(@"Please supply a new solution name");
                return;
            }

            if (_selectedProject == null)
            {
                MessageBox.Show(@"Please select a project");
                return;
            }
            if (_selectedSolution == null)
            {
                MessageBox.Show(@"Please supply a solution");
                return;
            }
            if (_selectedProject.Solutions.Count(p => p.SolutionName == NewNameTextBox.Text) > 0)
            {
                MessageBox.Show(@"A solution with this name already exists");
                return;
            }

            try
            {
                CopySolution();
                MessageBox.Show(@"The solution has been copied");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        private void LoadProjects()
        {
            ProjectsComboBox.DisplayMember = "ProjectName";
            ProjectsComboBox.ValueMember = "ProjectID";
            ProjectsComboBox.DataSource = _db.Projects;
            _selectedProject = (Project) ProjectsComboBox.SelectedItem;
            LoadSolutions();
        }

        private void LoadSolutions()
        {
            if (_selectedProject == null) return;
            SolutionsComboBox.DisplayMember = "SolutionName";
            SolutionsComboBox.ValueMember = "SolutionID";
            SolutionsComboBox.DataSource = _selectedProject.Solutions;
            _selectedSolution = (Solution) SolutionsComboBox.SelectedItem;
        }

        private void CopySolution()
        {
            var newSolution = new Solution
                                  {
                                      SolutionName = NewNameTextBox.Text,
                                      SolutionDescription = _selectedSolution.SolutionDescription
                                  };
            foreach (var setting in _selectedSolution.SolutionSettings)
            {
                var newSetting = new SolutionSetting
                                     {
                                         DefaultValue = setting.DefaultValue,
                                         SettingDescription = setting.SettingDescription,
                                         SettingTag = setting.SettingTag
                                     };

                foreach (var newSettingValue in setting.SettingValues.Select(settingValue => new SettingValue
                                                                                                 {
                                                                                                     EnvironmentID = settingValue.EnvironmentID, SettingValue1 = settingValue.SettingValue1, SolutionSettingID = settingValue.SolutionSettingID
                                                                                                 }))
                {
                    newSetting.SettingValues.Add(newSettingValue);
                }
                newSolution.SolutionSettings.Add(newSetting);
            }
            _selectedProject.Solutions.Add(newSolution);
            if (DataContext.ContainsChanges(_db))
                _db.SubmitChanges();
        }
    }
}